/**
 * Created by Administrator on 2017/6/30 0030.
 */
"use strict";

angular.module('app.yksensor').controller('YKHistoricaldataCtrl', function ($scope, sensorHttpS,$http) {

    //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [10, 20, 30, 50, 1000],
        onChange: function () {
        }
    };

    $scope.exportData = function () {

        var blob = new Blob(["\ufeff" + document.getElementById('exportable').innerHTML], {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
        });

        saveAs(blob, "Report.xls");
    };

    var opt=window.location.hash.split('/');
    if(opt[2])
    {
        console.log(opt[2]);
        $scope.paginationConf.deviceid = opt[2];
        $http.post(baseurls+"/list/select/yk_datasub?deviceid="+$scope.paginationConf.deviceid, $scope.paginationConf).then(function(res){
            // console.log(res.data)
            if(res.data.ret == 1){
                $scope.datas = res.data.rows;
                $scope.paginationConf.totalItems = res.data.records;
            }
            
        })
    }
    $scope.sends = function () {
    
        $scope.paginationConf.deviceid = $scope.paginationConf.deviceid == undefined ? "" : $scope.paginationConf.deviceid;
        
        var timefrom = $('#startdate').val();
        var timeto = $('#finishdate').val();
        if(timefrom == "" && timeto == ""){
            $scope.paginationConf.time = "";
        }else{
            $scope.paginationConf.time = $('#startdate').val()+","+$('#finishdate').val();
        }
        
        // $http.get(baseurls+"/list_nopage/log?deviceid="+$scope.paginationConf.deviceid+"&time="+$scope.paginationConf.time).then(function(res){
        //     console.log(res.data);
        //     if(res.data.ret == 1){
        //         $scope.datas = res.data.info;
        //         $scope.paginationConf.totalItems = res.data.info.length;
        //     }
        // })
        $http.post(baseurls+"/list/select/yk_datasub?deviceid="+$scope.paginationConf.deviceid+"&time="+$scope.paginationConf.time, $scope.paginationConf).then(function(res){
            // console.log(res.data)
            if(res.data.ret == 1){
                $scope.datas = res.data.rows;
                $scope.paginationConf.totalItems = res.data.records;

                for(var i=0;i<$scope.datas.length;i++){
                    $scope.datas[i].order = ($scope.paginationConf.currentPage-1)*10+i+1;//序号
                    if($scope.datas[i].sensor_num == 12){//温度计
                        var temps = $scope.datas[i].value.replace("[","").replace("]","").split(",");
                        for(var j=0;j<temps.length;j++){
                            temps[j] = parseFloat(temps[j])/Math.pow(10,$scope.datas[i].point_num);
                            if(temps[j] != 0){
                                if($scope.datas[i].dm == 1) temps[j] = -temps[j];
                            }
                        }
                        // console.log(temps);
                        $scope.datas[i].value = "["+temps.join("，")+"] °C";
                        // console.log($scope.datas[i].value);
                    }else if($scope.datas[i].sensor_num == 1){//液位计
                        var pos = $scope.datas[i].value.replace("[","").replace("]","");
                        pos = parseFloat(pos)/Math.pow(10,$scope.datas[i].point_num);
                        if(pos!=0){
                            if($scope.datas[i].dm == 1) pos = -pos;
                        }
                        $scope.datas[i].value = ""+pos.toString()+" m3";
                    }else if($scope.datas[i].sensor_num == 2){//流量计
                        var flow = $scope.datas[i].value.replace("[","").replace("]","").split(",");
                        for(var f=0;f<flow.length;f++){
                            flow[f] = parseFloat(flow[f])/Math.pow(10,$scope.datas[i].point_num);
                            if(flow[f] != 0){
                                if($scope.datas[i].dm == 1) flow[f] = -flow[f];
                            }
                        }
                        $scope.datas[i].value = "瞬时："+flow[0].toFixed(2)+" m3/s，"+"累计："+flow[1].toFixed(2)+" m3/s";
                    }
                }
            }
            
        })
    
    }

    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.sends);

});

